package cn.fuzongyao.learning.android.crime11;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CrimeLab {
    private static CrimeLab crimeLab;
    private List<Crime> crimes;

    public static CrimeLab getInstance() {
        if (crimeLab == null) {
            crimeLab = new CrimeLab();
        }
        return crimeLab;
    }

    private CrimeLab() {
        crimes = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            crimes.add(new Crime().setTitle("Crime #" + i).setSolved(i % 2 == 0));
        }
    }

    public List<Crime> getCrimes() {
        return crimes;
    }

    public Crime getCrime(UUID id) {
        return crimes.stream().filter(crime -> crime.getId().equals(id))
                .findFirst().orElse(null);
    }

    public int indexOf(UUID id) {
        for (int i = 0; i < crimes.size(); i++) {
            if (crimes.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
